-- 惊起春鸣
local jingqichunming = fk.CreateSkill {
    name = "mcdh_jingqichunming",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

jingqichunming:addEffect('viewas',{
    pattern = "slash,nullification",
    card_filter = function(self, player,to_select, selected)
        if player:getHandcardNum() > 1 then
            return #selected == 0 and Fk:currentRoom():getCardArea(to_select) ~= Card.PlayerEquip
        else
            return false
        end
    end,
    prompt = function (self,player)
        if player:getHandcardNum() > 1 then
            return "#mcdh_jingqichunming_discard:::"..self.interaction.data
        else
            return "#mcdh_jingqichunming_draw:::"..self.interaction.data
        end
    end,
    interaction = function(self,player)
        local names = {}
        local card
        card = Fk:cloneCard("slash")
        if ((Fk.currentResponsePattern == nil and player:canUse(card)) or (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card))) then
            table.insert(names,"slash")
        end
        card = Fk:cloneCard("nullification")
        if ((Fk.currentResponsePattern == nil and player:canUse(card)) or (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card))) then
            table.insert(names,"nullification")
        end
        if #names == 0 then
            return nil
        end
        return UI.ComboBox {choices = names}
    end,
    view_as = function(self, player,cards)
        if player:getHandcardNum() > 1 and #cards ~= 1 then
            return nil
        end
        local card
        card = Fk:cloneCard(self.interaction.data)
        card.skillName = self.name
        card:setMark(self.name, #cards == 0 and 0 or cards[1])
        return card
    end,
    enabled_at_response = function(self, player, response)
        return not response and #player:getCardIds(Player.Hand) ~= 1 and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0
    end,
    enabled_at_play = function(self, player)
        return #player:getCardIds(Player.Hand) ~= 1 and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0
    end,
    before_use = function(self, player, use)
        local room = player.room
        local n = player:getHandcardNum() - 1
        if n > 0 then
            local testid = use.card:getMark(self.name)
            room:throwCard(table.filter(player.player_cards[Player.Hand], function(p) return p ~= testid end),self.name,player)
        elseif n <0 then
            player:drawCards(-n, self.name)
        end
        local card = player:getCardIds(Player.Hand)
        use.extra_data = use.extra_data or {}
        use.extra_data.mcdh_JQCM = card
        player:showCards(card)
    end,
    after_use = function(self, player, use)
        local room = player.room
        local events =  player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
            for _, move in ipairs(e.data) do
                if move.toArea == Card.DiscardPile then
                    for _, info in ipairs(move.moveInfo) do
                        if use.extra_data and use.extra_data.mcdh_JQCM then
                            for _, cardid in ipairs(use.extra_data.mcdh_JQCM) do
                                if Fk:getCardById(info.cardId).suit == Fk:getCardById(cardid).suit then
                                    return true
                                end
                            end
                        end
                    end
                end
            end
        end, Player.HistoryTurn)
        if #events == 0 then
            local targets = {}
            for _, p in ipairs(room.alive_players) do
                if #targets == 0 then
                    targets = {p}
                else
                    if p:getHandcardNum() < targets[1]:getHandcardNum() then
                        targets = {p}
                    elseif p:getHandcardNum() == targets[1]:getHandcardNum() then
                        table.insert(targets,p)
                    end
                end
            end
            local p = room:askToChoosePlayers(player, {targets= targets,min_num=1,max_num=1,prompt="#jingqichunming-choose",skill_name=self.name,cancelable=false})[1]
            p:drawCards(2,self.name)
        end
    end,
})

jingqichunming:addEffect(fk.AfterCardsMove,{
    can_refresh = function(self, event, target, player, data)
        return player:hasSkill("mcdh_jingqichunming", true)
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local suitsRecorded = player:getTableMark("@jingqichunming-turn")
        local mark_change = false
        for _, move in ipairs(data) do
            if move.toArea == Card.DiscardPile then
                for _, info in ipairs(move.moveInfo) do
                    local suit = Fk:getCardById(info.cardId):getSuitString(true)
                    if not table.contains(suitsRecorded, suit) then
                        mark_change = true
                        table.insert(suitsRecorded, suit)
                    end
                end
            end
        end
        if mark_change then
            room:setPlayerMark(player, "@jingqichunming-turn", suitsRecorded)
        end
    end
})

Fk:loadTranslationTable{
    ["mcdh_jingqichunming"] ="惊起春鸣",
    ["@jingqichunming-turn"] = "惊起春鸣",
    [":mcdh_jingqichunming"] = "每回合限一次，若你的手牌数不为1，你可以将手牌数调整至1并展示之，然后视为使用一张【杀】或【无懈可击】。若本回合进入弃牌堆的牌中不包含展示牌的花色 ，你令手牌数最少的一名角色摸两张牌。",
    ["#jingqichunming-choose"] = "惊起春鸣:令手牌数最少的一名角色摸两张牌",
    ["#mcdh_jingqichunming_discard"] = "惊起春鸣:你可以选择一张手牌并弃置其余手牌，视为使用【%arg】",
    ["#mcdh_jingqichunming_draw"] = "惊起春鸣:你可以摸一张牌，视为使用【%arg】"
}

return jingqichunming